package com.hugh.dubbo.data;

import org.apache.dubbo.common.utils.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: Hugh
 * @Date: 2020/7/5
 */
public class MonitorData {

    public static Integer HEALTH_CODE = 0;
    public static Integer BAD_STATUS = -1;

    /**
     * kye:path,
     * val:TreeMap  key:long 插入时间,val:接口响应时间(-1则是接口响应失败)
     */
    private static ConcurrentHashMap<String, TreeMap<Long, Long>> dataMap;

    /**
     * save
     * @param path 路径
     * @param rt 响应时间
     */
    public static void saveMonitorData(String path, Long rt){
        if(Objects.isNull(dataMap)){
            dataMap = new ConcurrentHashMap<>();
        }
        if(dataMap.containsKey(path)){
            TreeMap<Long, Long> treeMap = dataMap.get(path);
            if(CollectionUtils.isNotEmptyMap(treeMap)){
                treeMap.put(System.currentTimeMillis(), rt);
            }else{
                treeMap = new TreeMap<>();
                treeMap.put(System.currentTimeMillis(), rt);
            }
            dataMap.put(path, treeMap);
        }else{
            TreeMap<Long, Long> longLongLinkedHashMap = new TreeMap<>();
            longLongLinkedHashMap.put(System.currentTimeMillis(), rt);
            dataMap.put(path, longLongLinkedHashMap);
        }
    }

    /**
     *  获取接口最近几分钟的响应数据
     * @param millis 最近毫秒
     * @return  Map<String, List<Long>> kay:path, val:rt集合
     */
    public static Map<String, List<Long>> getLatestRtCollention(Long millis){
        if(Objects.isNull(millis) || CollectionUtils.isEmptyMap(dataMap)){
            return Collections.emptyMap();
        }
        //获取开始时间的key
        long keyStartTimeMillis = System.currentTimeMillis() - millis;
        Map<String, List<Long>> resMap = new HashMap<>();
        Iterator<Map.Entry<String, TreeMap<Long, Long>>> iterator = dataMap.entrySet().iterator();
        while (iterator.hasNext()) {
            List<Long> pathRtList = new ArrayList<>();
            Map.Entry<String, TreeMap<Long, Long>> next = iterator.next();
            TreeMap<Long, Long> treeMapValue = next.getValue();
            if(!treeMapValue.containsKey(keyStartTimeMillis)){
                Long aLong = treeMapValue.lastKey();
                if(keyStartTimeMillis > aLong){
                    continue;
                }
                //如果不包含key，则取大于该key的最小key
                keyStartTimeMillis = treeMapValue.higherKey(keyStartTimeMillis);
            }
            SortedMap<Long, Long> sortedMap = treeMapValue.tailMap(keyStartTimeMillis);
            for (Map.Entry<Long, Long> next1 : sortedMap.entrySet()) {
                pathRtList.add(next1.getValue());
            }
            resMap.put(next.getKey(), pathRtList);
        }
        return resMap;
    }
}
